package com.dongdongshop.aop;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect //声明为切面类
@Slf4j
public class LogAop {
    //切点
    @Pointcut("execution(* com.dongdongshop.service.impl.*.*(..)) || @annotation(com.dongdongshop.anon.LogNote)")
    public void pointCut(){}

    /**
     * 前置通知
     * @param joinPoint
     */
    @Before("pointCut()")//切点选择
    public void before(JoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();//获得被拦截的方法的参数
        Signature signature = joinPoint.getSignature();
        log.info("当前正在执行的方法是：{}，参数是：{}",signature,args);
    }


    /**
     * 返回后通知
     */
    @AfterReturning(pointcut = "pointCut()",returning ="result")    //returning 返回值类型
    public void afterReturning(Object result) throws Throwable{
        log.info("方法执行成功，返回的结果为：{}", JSONUtil.toJsonStr(result));
    }
}